package com.example.peach;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class PeachActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView peach_1,peach_2,peach_3,peach_4,peach_5,peach_6;
    private Button btn_exit;
    private int count = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_peach);
        init();
    }

    private void init() {
        peach_1 = findViewById(R.id.peach_1);
        peach_2 = findViewById(R.id.peach_2);
        peach_3 = findViewById(R.id.peach_3);
        peach_4 = findViewById(R.id.peach_4);
        peach_5 = findViewById(R.id.peach_5);
        peach_6 = findViewById(R.id.peach_6);
        btn_exit = findViewById(R.id.btn_exit);
        peach_1.setOnClickListener(this);
        peach_2.setOnClickListener(this);
        peach_3.setOnClickListener(this);
        peach_4.setOnClickListener(this);
        peach_5.setOnClickListener(this);
        peach_6.setOnClickListener(this);
        btn_exit.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.peach_1) {
            info(peach_1);
        } else if (view.getId() == R.id.peach_2) {
            info(peach_2);

        } else if (view.getId() == R.id.peach_3) {
            info(peach_3);

        } else if (view.getId() == R.id.peach_4) {
            info(peach_4);

        } else if (view.getId() == R.id.peach_5) {
            info(peach_5);

        } else if (view.getId() == R.id.peach_6) {
            info(peach_6);

        } else if (view.getId() == R.id.btn_exit) {
            returnDate();   //退出桃园点击事件

        }


    }

    //按钮的点击事件处理
    private void info(ImageView imageView) {
        imageView.setVisibility(View.INVISIBLE);
        count++;
        Toast.makeText(PeachActivity.this, "摘到" + count + "个桃子", Toast.LENGTH_LONG).show();

    }


    //将数据回传到上一个页面
    private void returnDate() {
        Intent intent = new Intent();
        intent.putExtra("count", count);
        setResult(1, intent);
        PeachActivity.this.finish();
    }



    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            returnDate();
        }
        return false;


    }

    }